package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQueryVo;
import com.atguigu.eduservice.mapper.TeacherMapper;
import com.atguigu.eduservice.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2020-12-25
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, EduTeacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 根据id查询教师
     *
     * @param id
     * @return
     */
    @Override
    public EduTeacher get(String id) {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 教师带条件的分页查询
     *
     * @param current
     * @param limit
     * @param teacherQueryVo
     * @return
     */
    @Override
    public Map<String, Object> page(Long current, Long limit, TeacherQueryVo teacherQueryVo) {
        Page pageTeacher = selectTeachersByConditions(current, limit, teacherQueryVo);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageTeacher.getTotal());
        map.put("rows", pageTeacher.getRecords());
        return map;
    }

    /**
     * 前台教师信息分页
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map<String, Object> getTeacherFrontList(long page, long limit) {
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        QueryWrapper<EduTeacher> qw = new QueryWrapper<>();
        qw.orderByDesc("id");
        baseMapper.selectPage(pageParam, qw);

        Map<String,Object> map = new HashMap<>();
        map.put("records",pageParam.getRecords());
        map.put("current",pageParam.getCurrent());
        map.put("pages",pageParam.getPages());
        map.put("size",pageParam.getSize());
        map.put("total",pageParam.getTotal());
        map.put("hasNext",pageParam.hasNext());
        map.put("hasPrevious",pageParam.hasPrevious());

        return map;
    }

    /**
     * 根据条件查询教师
     * @param current
     * @param limit
     * @param teacherQueryVo
     * @return
     */
    public Page selectTeachersByConditions(Long current, Long limit, TeacherQueryVo teacherQueryVo) {
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        QueryWrapper<EduTeacher> qw = new QueryWrapper<>();
        if (teacherQueryVo!=null){
          qw.likeRight(teacherQueryVo.getName() != null, "name", teacherQueryVo.getName());
          qw.eq(teacherQueryVo.getLevel() != null, "level", teacherQueryVo.getLevel());
          qw.ge(teacherQueryVo.getBegin() != null, "gmt_create", teacherQueryVo.getBegin());
          qw.ge(teacherQueryVo.getEnd() != null, "gmt_modified", teacherQueryVo.getEnd());
          qw.orderByDesc("sort");
        }
        return (Page) baseMapper.selectPage(pageTeacher, qw);
    }



}
